package com.sjhua.study.timerutil;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {
	private final String TAG = "MainActivity";
	private Button ShowDataPickerButton = null;
	private Button ShowClockPickerButton = null;
	private Button btn_showsystemsetup = null;
	private static final int DATA_PICKER_ID = 1;
	private static final int CLOCK_PICKER_ID = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ShowDataPickerButton = (Button) findViewById(R.id.ShowDataPickerButton);
		ShowDataPickerButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DATA_PICKER_ID);
			}
		});

		ShowClockPickerButton = findViewById(R.id.ShowClockPIckerButton);
		ShowClockPickerButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(CLOCK_PICKER_ID);
			}
		});

		btn_showsystemsetup = findViewById(R.id.ShowSystemSetup);
		btn_showsystemsetup.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction("android.settings.DATE_SETTINGS");
				startActivity(intent);
			}
		});
	}

	DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			Log.i(TAG, "Set:"+year + "-" + monthOfYear + "-" + dayOfMonth);
			System.out.print(year + "-" + monthOfYear + "-" + dayOfMonth + " ");
			//实现修改系统日期功能
		}
	};

	TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			Log.i(TAG, "Set Time:" + hourOfDay + ":" + minute);
			System.out.println(hourOfDay + ":" + minute);
			//实现修改系统时间功能
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
			case DATA_PICKER_ID:
				return new DatePickerDialog(this, onDateSetListener, 2020, 11,01);
			case CLOCK_PICKER_ID:
				return new TimePickerDialog(this,onTimeSetListener,10,8,true);
		}
		return super.onCreateDialog(id);
	}
}